package org.caro.actions.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.caro.actions.ContestantAction;
import org.caro.actions.transfer.ControlGame;
import org.caro.gui.MainGame;
import org.caro.gui.MenuGui;
import org.caro.gui.WaitingRoom;
import org.caro.utils.Constants;

public class WaitingRoomListener implements ActionListener {
	private WaitingRoom waitingRoom;
	private ControlGame controlSend, controlRecv;

	public WaitingRoomListener(WaitingRoom waitingRoom) {
		this.waitingRoom = waitingRoom;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.waitingRoom.getQuickJoin()) {
			connectToServer(getQuickJoinIP());
		} else if (e.getSource() == this.waitingRoom.getJoin()) {
			String  ip = JOptionPane.showInputDialog(null, "IP:");
			connectToServer(getJoinByIP(ip));
		} else if (e.getSource() == this.waitingRoom.getCreate()) {
			createHost();

		} else if (e.getSource() == this.waitingRoom.getExit()) {
			MenuGui gui = MenuGui.getInstance();
			gui.setVisible(true);
			this.waitingRoom.dispose();
		}

	}

	private void connectToServer(ControlGame control){
		if (control == null){
			JOptionPane.showMessageDialog(null, "FAILURE");
		}
		this.controlSend = control;
		this.controlRecv = new ControlGame(control.getIP(), Constants.PORT_RECEIVE_BY_CLIENT, Constants.CLIENT);
		this.controlSend.sentData(this.waitingRoom.getPlayerName());
		String name = this.controlRecv.receiveData();
		ContestantAction contestant;
		try {
			contestant = new ContestantAction(
					this.waitingRoom.getPlayerName(), name, false,
					Constants.HOST, "", Constants.PORT_CHAT_SEND_CLIENT,
					Constants.PORT_CHAT_RECV_CLIENT,
					Constants.PORT_HIT_SEND_CLIENT,
					Constants.PORT_HIT_RECV_CLIENT, controlSend,
					controlRecv);
			new MainGame(Constants.LAN_MODE, contestant);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void createHost()  {
		this.controlSend = new ControlGame("", Constants.PORT_SEND_BY_SERVER,
				Constants.HOST,this);
		this.controlRecv = new ControlGame("",
				Constants.PORT_RECEIVE_BY_SERVER, Constants.HOST,this);
		controlSend.start();
		controlRecv.start();
		try {
			wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name = controlRecv.receiveData();
		controlSend.sentData(this.waitingRoom.getPlayerName());
		try {
			ContestantAction contestant = new ContestantAction(
					this.waitingRoom.getPlayerName(), name, true,
					Constants.HOST, "", Constants.PORT_CHAT_SEND_HOST,
					Constants.PORT_CHAT_RECV_HOST,
					Constants.PORT_HIT_SEND_HOST,
					Constants.PORT_HIT_RECV_HOST, controlSend,
					controlRecv);
			new MainGame(Constants.LAN_MODE, contestant);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private ControlGame getQuickJoinIP() {
		String localIp = "";
		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
		}
		byte[] ipAddr = addr.getAddress();
		for (int i = 0; i < ipAddr.length - 1; i++) {
			if (ipAddr[i] < 0)
				localIp = localIp + String.valueOf(ipAddr[i]) + ".";
		}

		for (int i = 0; i < 256; i++) {
			try {
				String lanIp = localIp + String.valueOf(i);
				ControlGame control = new ControlGame(lanIp,
						Constants.PORT_RECEIVE_BY_CLIENT, Constants.CLIENT);
				control.initConnection();
				return control;
			} catch (Exception ex) {
				System.out.println("Failure to try connect to " + localIp + i);
			}
		}
		return null;
	}

	private ControlGame getJoinByIP(String ip) {
		ControlGame control = new ControlGame(ip,
				Constants.PORT_SEND_BY_CLIENT, Constants.CLIENT);
		try {
			control.initConnection();
			return control;
		} catch (Exception e) {
			System.out.println("Failure to try connect to " + ip);
		}
		return null;
	}
}
